﻿using BigEgg.Framework.Applications.ViewModels;
using FMStudio.Documents.TemplateDocument;
using FMStudio.Templates.DesignTool.Application.Services;
using FMStudio.Templates.DesignTool.Application.Views;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;

namespace FMStudio.Templates.DesignTool.Application.ViewModels
{
    [Export]
    public class ProjectExplorerViewModel : ViewModel<IProjectExploreView>
    {
        #region Fields
        private readonly IShellService shellService;
        private readonly IFileService fileService;

        private readonly ObservableCollection<object> documentViews;
        private object activeDocumentView;
        private object startView;

        private TemplateDocument activeDocument;
        #endregion

        [ImportingConstructor]
        public ProjectExplorerViewModel(IProjectExploreView view, IShellService shellService, IFileService fileService)
            : base(view)
        {
            this.shellService = shellService;
            this.fileService = fileService;

            this.documentViews = new ObservableCollection<object>();

            AddWeakEventListener(this.documentViews, DocumentViewsCollectionChanged);
            AddWeakEventListener(this.fileService, FileServicePropertyChanged);
        }

        #region Properties
        public IFileService FileService { get { return this.fileService; } }

        public ObservableCollection<object> DocumentViews { get { return this.documentViews; } }

        public object ActiveDocumentView
        {
            get { return this.activeDocumentView; }
            set
            {
                if (this.activeDocumentView != value)
                {
                    this.activeDocumentView = value;
                    RaisePropertyChanged("ActiveDocumentView");
                }
            }
        }

        public object StartView
        {
            get { return this.startView; }
            set
            {
                if (this.startView != value)
                {
                    this.startView = value;
                    RaisePropertyChanged("StartView");
                }
            }
        }
        #endregion

        #region Methods
        #region Private Methods
        private void DocumentViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!this.documentViews.Any())
            {
                ViewCore.ContentViewState = ContentViewState.StartViewVisible;
            }
            else
            {
                ViewCore.ContentViewState = ContentViewState.DocumentViewVisible;
            }
        }

        private void FileServicePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActiveDocument")
            {
                if (this.activeDocument != null) { RemoveWeakEventListener(this.activeDocument, ActiveDocumentPropertyChanged); }

                this.activeDocument = this.fileService.ActiveDocument;

                if (this.activeDocument != null) { AddWeakEventListener(this.activeDocument, ActiveDocumentPropertyChanged); }

                UpdateShellServiceDocumentName();
            }
        }

        private void ActiveDocumentPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FilePath")
            {
                UpdateShellServiceDocumentName();
            }
        }

        private void UpdateShellServiceDocumentName()
        {
            if (this.activeDocument != null)
            {
                this.shellService.DocumentName = Path.GetFileNameWithoutExtension(this.activeDocument.FilePath);
            }
            else
            {
                this.shellService.DocumentName = string.Empty;
            }
        }
        #endregion
        #endregion
    }
}
